package cn.pping.pet.controller;

import cn.pping.basic.annotation.PreAuthorize;
import cn.pping.basic.exception.BusinessExeption;
import cn.pping.basic.util.LoginContext;
import cn.pping.pet.dto.AcceptDto;
import cn.pping.pet.service.ISearchMasterMsgService;
import cn.pping.pet.domain.SearchMasterMsg;
import cn.pping.pet.query.SearchMasterMsgQuery;
import cn.pping.basic.util.JsonResult;
import cn.pping.basic.util.PageList;
import cn.pping.user.domain.Logininfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* @Description: 寻主类
* @Author: Pan
* @Version: 1.0
* @Date:  2022/8/11 14:37
*/

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;


    /**
     * 保存和修改公用的
     *
     * @param searchMasterMsg 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "寻主新增修改", sn = "smm:put")
    public JsonResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
            if (searchMasterMsg.getId() != null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    @PreAuthorize(name = "寻主根据id删除", sn = "smm:delete")
    public JsonResult delete(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "寻主根据id查询", sn = "smm:get")
    public SearchMasterMsg get(@PathVariable("id") Long id) {
        return searchMasterMsgService.loadById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<SearchMasterMsg> list() {

        return searchMasterMsgService.loadAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    @PreAuthorize(name = "寻主分页", sn = "smm:post")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.pageQuery(query);
    }


    /**
     * 寻主信息
     *
     * @param request 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/publish")
    @PreAuthorize(name = "寻主", sn = "smm:post")
    public JsonResult publish(@RequestBody SearchMasterMsg ssm, HttpServletRequest request) {
        try {
            Logininfo loginInfo = LoginContext.getLoginInfo(request);
            searchMasterMsgService.publish(ssm, loginInfo.getId());
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("发布消息失败" + e.getMessage());
        }
    }

    /**
     *  待处理
     * @return
     */
    @ApiOperation(value = "待处理查询接口" )
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLoginInfo(request);
            return searchMasterMsgService.toHandle(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }

    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/toAudit")
    @PreAuthorize(name = "寻主待审核", sn = "smm:post")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        try {
            Logininfo logininfo = LoginContext.getLoginInfo(request);
            return searchMasterMsgService.toAudit(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }


    /**
     *  用户处理
     * @return
     */
    @ApiOperation(value = "待处理查询接口" )
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {

            Logininfo logininfo = LoginContext.getLoginInfo(request);
            return searchMasterMsgService.user(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }

    /**
     * 拒单
     * @param
     * @return
     */
    @GetMapping("/reject/{id}")
    @ApiOperation(value = "拒单" )
    public JsonResult reject(@PathVariable("id") Long id ){

        try {
            searchMasterMsgService.reject(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    /**
     * 接单
     * @param
     * @return
     */
    @PostMapping("/accept")
    @ApiOperation(value = "接单" )
    public JsonResult accept(@RequestBody AcceptDto dto, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLoginInfo(request);
            searchMasterMsgService.accept(dto,logininfo.getId());
            return JsonResult.me();
        }catch (BusinessExeption e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

}
